using System;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class BumpySurface : MonoBehaviour
{
	public GameObject bumpPrefab;

	private float surfaceHeight = 1f;

	private float surfaceWidth = 1f;

	private float surfaceLenght = 1f;

	public float bumpSize = 1f;

	public float bumpHeight;

	public float bumpDistace = 1f;

	public Vector3 scaleFactor = Vector3.one;

	private float surfaceHeightOld = 1f;

	private float surfaceWidthOld = 1f;

	private float surfaceLenghtOld = 1f;

	private float bumpSizeOld = 1f;

	private float bumpHeightOld;

	private float bumpDistaceOld = 1f;

	private bool freeze;

	[HideInInspector]
	public List<GameObject> bumps;

	[HideInInspector]
	public List<bool> bumpsUsedState;

	[HideInInspector]
	public GameObject bumpsParent;

	private void Start()
	{
	}

	public void FreezeBumps()
	{
		freeze = true;
	}

	private void OnEnable()
	{
		if (!freeze && Application.isEditor && !Application.isPlaying)
		{
			Refresh(true);
		}
	}

	private void OnDisable()
	{
		if (!freeze && Application.isEditor && !Application.isPlaying)
		{
			UnuseAllBumps();
			ClearBumpPool();
		}
	}

	private void Update()
	{
		if (!freeze && Application.isEditor && !Application.isPlaying)
		{
			Refresh();
		}
	}

	private void Refresh(bool forced = false)
	{
		if (freeze)
		{
			return;
		}
		if (bumpPrefab == null)
		{
			Debug.Log("!!! BumpySurface: bump prefab not found");
			return;
		}
		bool flag = forced;
		surfaceWidth = base.transform.localScale.x * scaleFactor.x;
		surfaceHeight = base.transform.localScale.y * scaleFactor.y;
		surfaceLenght = base.transform.localScale.z * scaleFactor.z;
		if (bumpsParent == null)
		{
			flag = true;
		}
		if (surfaceWidth >= bumpSize && surfaceLenght >= bumpSize)
		{
			SetBumpsParent();
			bumpsParent.transform.parent = base.transform.parent;
			bumpsParent.transform.position = base.transform.position;
			bumpsParent.transform.rotation = base.transform.rotation;
		}
		if (surfaceWidth != surfaceWidthOld)
		{
			flag = true;
		}
		if (surfaceHeight != surfaceHeightOld)
		{
			flag = true;
		}
		if (surfaceLenght != surfaceLenghtOld)
		{
			flag = true;
		}
		if (bumpDistace != bumpDistaceOld)
		{
			flag = true;
		}
		if (bumpSize != bumpSizeOld)
		{
			flag = true;
		}
		if (bumpHeight != bumpHeightOld)
		{
			flag = true;
		}
		surfaceWidthOld = surfaceWidth;
		surfaceHeightOld = surfaceHeight;
		surfaceLenghtOld = surfaceLenght;
		bumpDistaceOld = bumpDistace;
		bumpSizeOld = bumpSize;
		bumpHeightOld = bumpHeight;
		if (flag)
		{
			SetBumps();
		}
	}

	private void SetBumpsParent()
	{
		if (bumpsParent == null)
		{
			bumpsParent = new GameObject(base.name + " - BUMPS");
			bumpsParent.AddComponent<DestroyIfNoChildren>();
		}
	}

	private void InitBumps()
	{
		if (bumps == null)
		{
			bumps = new List<GameObject>();
			bumpsUsedState = new List<bool>();
		}
	}

	private GameObject NewBump()
	{
		InitBumps();
		for (int i = 0; i < bumpsUsedState.Count; i++)
		{
			if (!bumpsUsedState[i])
			{
				bumpsUsedState[i] = true;
				if (bumps[i] == null)
				{
					bumps[i] = CreateBump();
				}
				return bumps[i];
			}
		}
		bumps.Add(CreateBump());
		bumpsUsedState.Add(true);
		return bumps[bumpsUsedState.Count - 1];
	}

	private GameObject CreateBump()
	{
		if (bumpPrefab == null)
		{
			return null;
		}
		SetBumpsParent();
		GameObject gameObject = UnityEngine.Object.Instantiate(bumpPrefab) as GameObject;
		gameObject.name = "bump";
		gameObject.transform.parent = bumpsParent.transform;
		gameObject.transform.localEulerAngles = new Vector3(-90f, UnityEngine.Random.Range(0, 60) * 6, 0f);
		return gameObject;
	}

	private void ClearBumpPool()
	{
		if (bumps == null)
		{
			return;
		}
		int num = 999;
		while (num >= 0)
		{
			num = -1;
			for (int i = 0; i < bumpsUsedState.Count; i++)
			{
				if (!bumpsUsedState[i])
				{
					num = i;
					break;
				}
			}
			if (num >= 0)
			{
				UnityEngine.Object.DestroyImmediate(bumps[num]);
				bumps.RemoveAt(num);
				bumpsUsedState.RemoveAt(num);
			}
		}
	}

	private void UnuseAllBumps()
	{
		if (bumps != null)
		{
			for (int i = 0; i < bumpsUsedState.Count; i++)
			{
				bumpsUsedState[i] = false;
			}
		}
	}

	private void SetBumps()
	{
		UnuseAllBumps();
		if (surfaceWidth >= bumpSize && surfaceLenght >= bumpSize)
		{
			float num = bumpDistace * Mathf.Cos((float)Math.PI / 6f);
			int num2 = (int)((surfaceWidth / 2f - bumpSize / 2f) / bumpDistace);
			int num3 = (int)((surfaceWidth / 2f - bumpDistace / 2f - bumpSize / 2f) / bumpDistace) + 1;
			int num4 = (int)((surfaceLenght / 2f - bumpSize / 2f) / num);
			for (int i = 0; i <= num4; i++)
			{
				if (i % 2 == 0)
				{
					GameObject gameObject = NewBump();
					gameObject.transform.localScale = Vector3.one * bumpSize;
					gameObject.transform.localPosition = new Vector3(0f, surfaceHeight / 2f + bumpHeight, (float)i * num);
					if (i > 0)
					{
						gameObject = NewBump();
						gameObject.transform.localScale = Vector3.one * bumpSize;
						gameObject.transform.localPosition = new Vector3(0f, surfaceHeight / 2f + bumpHeight, (float)(-i) * num);
					}
					for (int j = 1; j <= num2; j++)
					{
						gameObject = NewBump();
						gameObject.transform.localScale = Vector3.one * bumpSize;
						gameObject.transform.localPosition = new Vector3((float)j * bumpDistace, surfaceHeight / 2f + bumpHeight, (float)i * num);
						gameObject = NewBump();
						gameObject.transform.localScale = Vector3.one * bumpSize;
						gameObject.transform.localPosition = new Vector3((float)(-j) * bumpDistace, surfaceHeight / 2f + bumpHeight, (float)i * num);
						if (i > 0)
						{
							gameObject = NewBump();
							gameObject.transform.localScale = Vector3.one * bumpSize;
							gameObject.transform.localPosition = new Vector3((float)j * bumpDistace, surfaceHeight / 2f + bumpHeight, (float)(-i) * num);
							gameObject = NewBump();
							gameObject.transform.localScale = Vector3.one * bumpSize;
							gameObject.transform.localPosition = new Vector3((float)(-j) * bumpDistace, surfaceHeight / 2f + bumpHeight, (float)(-i) * num);
						}
					}
				}
				else
				{
					for (int k = 0; k < num3; k++)
					{
						GameObject gameObject = NewBump();
						gameObject.transform.localScale = Vector3.one * bumpSize;
						gameObject.transform.localPosition = new Vector3(bumpDistace / 2f + (float)k * bumpDistace, surfaceHeight / 2f + bumpHeight, (float)i * num);
						gameObject = NewBump();
						gameObject.transform.localScale = Vector3.one * bumpSize;
						gameObject.transform.localPosition = new Vector3((0f - bumpDistace) / 2f - (float)k * bumpDistace, surfaceHeight / 2f + bumpHeight, (float)i * num);
						gameObject = NewBump();
						gameObject.transform.localScale = Vector3.one * bumpSize;
						gameObject.transform.localPosition = new Vector3(bumpDistace / 2f + (float)k * bumpDistace, surfaceHeight / 2f + bumpHeight, (float)(-i) * num);
						gameObject = NewBump();
						gameObject.transform.localScale = Vector3.one * bumpSize;
						gameObject.transform.localPosition = new Vector3((0f - bumpDistace) / 2f - (float)k * bumpDistace, surfaceHeight / 2f + bumpHeight, (float)(-i) * num);
					}
				}
			}
		}
		ClearBumpPool();
	}
}
